import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Form {

	private JFrame frame;
	private JTable table;
	private JPanel panel;
	int rows = 100;
	int rowheight = 20;
	int fieldheight = 40;
	int colwidth = 120;

	List<String> fields_ = new ArrayList<String>();
	TableModel model;

	private class RowListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent event) {
			if (event.getValueIsAdjusting()) {
				return;
			}
			int r = table.getSelectionModel().getLeadSelectionIndex();
			for (int i = 0; i < fields.size(); i++) {
				JTextField f = (JTextField) fields.get(i);
				f.setText(model.getValueAt(r, i).toString());
			}
			//System.out.println("row "
			//		+ table.getSelectionModel().getLeadSelectionIndex());
		}
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Form window = new Form();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Form() {
		initialize();
	}

	private void initialize() {

		model = createTableModel();

		frame = new JFrame();
		frame.setBounds(100, 100, fields_.size() * colwidth,
				(fields_.size() * fieldheight) + (rows * rowheight));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new GridLayout(0, 1, 0, 0));

		JSplitPane splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);

		GridBagConstraints gbc_splitPane = new GridBagConstraints();
		gbc_splitPane.fill = GridBagConstraints.BOTH;
		gbc_splitPane.gridx = 0;
		gbc_splitPane.gridy = 0;
		frame.getContentPane().add(splitPane);

		JScrollPane listPane = new JScrollPane();
		splitPane.setRightComponent(listPane);

		table = new JTable();
		table.setModel(model);
		table.getSelectionModel().addListSelectionListener(new RowListener());
		listPane.setViewportView(table);
		panel = new JPanel();
		splitPane.setLeftComponent(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 150, 300, 0, 0 };
		gbl_panel.rowHeights = new int[] { rowheight, 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 1.0, 0.0,
				Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);
		for (int i = 0; i < fields_.size(); i++) {
			addFormField(fields_.get(i), RandomString.generateStringAlpha(10),
					i);

		}
	}

	TableModel createTableModel() {
		for (int i = 0; i < 5; i++) {
			fields_.add(RandomString.generateStringAlpha(5));
		}
		TableModel mtm = new TableModel();
		for (int j = 0; j < rows; j++) {
			List<String> ro = new ArrayList<String>();
			for (int i = 0; i < fields_.size(); i++) {
				ro.add(RandomString.generateStringAlpha(10));
			}
			mtm.addRow(ro);
		}
		List<String> co = new ArrayList<String>();
		for (int i = 0; i < fields_.size(); i++) {
			co.add(fields_.get(i));
		}
		mtm.setColumns(co);
		return mtm;
	}

	List<JComponent> fields = new ArrayList<JComponent>();

	void addFormField(String label, String text, int y) {
		JLabel lb = new JLabel(label);
		GridBagConstraints gbc1 = new GridBagConstraints();
		gbc1.anchor = GridBagConstraints.EAST;
		gbc1.insets = new Insets(0, 0, 5, 5);
		gbc1.gridx = 0;
		gbc1.gridy = y;
		panel.add(lb, gbc1);

		JTextField txt = new JTextField();
		fields.add(txt);
		txt.setText(text);
		GridBagConstraints gbc2 = new GridBagConstraints();
		gbc2.insets = new Insets(0, 0, 5, 5);
		gbc2.fill = GridBagConstraints.HORIZONTAL;
		gbc2.gridx = 1;
		gbc2.gridy = y;
		panel.add(txt, gbc2);
		txt.setColumns(10);
	}

}
